package com.itcast.tanhuaserver.controller;

import com.itcast.tanhuaserver.service.QuestionService;
import com.itcast.tanhuaserver.service.TodayBestService;
import com.itcast.tanhuaserver.utils.Cache;
import com.itcast.tanhuaserver.vo.NearUserVo;
import com.itcast.tanhuaserver.vo.PageResult;
import com.itcast.tanhuaserver.vo.RecommendUserQueryParam;
import com.itcast.tanhuaserver.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.repository.Near;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/1610:07
 * @Version 1.0
 */
@RestController
@RequestMapping("tanhua")
@Slf4j
public class TodayBestController {

    @Autowired
    private TodayBestService todayBestService;

    /**
     * 查询今日佳人信息
     * @param token
     * @return
     */
    @GetMapping("todayBest")
    public ResponseEntity<TodayBest> queryTodayBest(@RequestHeader("Authorization") String token){
        try {
            TodayBest todayBest = this.todayBestService.queryTodayBest(token);
            if (todayBest != null){
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            log.error("查询今日佳人错误 ~  token :" + token,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 查询用户推荐列表
     * @param token
     * @param recommendUserQueryParam
     * @return
     */
    @GetMapping("recommendation")
    @Cache
    public ResponseEntity<PageResult> queryRecommendation(@RequestHeader("Authorization") String token, RecommendUserQueryParam recommendUserQueryParam){
        try {
            PageResult pageResult = this.todayBestService.queryRecommendation(token,recommendUserQueryParam);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询用户推荐列表错误 ~  token :" + token + ",recommendUserQueryParam = " + recommendUserQueryParam,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 查询今日佳人详情
     *
     * @param userId
     * @return
     */
    @GetMapping("{id}/personalInfo")
    public ResponseEntity<TodayBest> queryTodayBest(@PathVariable("id") Long userId){
        try {
            TodayBest todayBest = this.todayBestService.queryTodayBest(userId);
            if (null != todayBest){
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            log.error("查询今日佳人详情错误 ~  userId :" + userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId){
        try {
            String question = this.todayBestService.queryQuestion(userId);
            if (!StringUtils.isEmpty(question)){
                return ResponseEntity.ok(question);
            }
        } catch (Exception e) {
            log.error("查询陌生人问题错误 ~  userId :" + userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回复陌生人问题
     *
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replyQuestion(@RequestBody Map<String, Object> param){
        Long userId = Long.valueOf(param.get("userId").toString());
        String reply = param.get("reply").toString();
        try {
            boolean bool = this.todayBestService.replyQuestion(userId,reply);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (NumberFormatException e) {
            log.error("回复陌生人问题错误 ~  userId :" + userId + ",reply=" + reply,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender", required = false) String gender,
                                                    @RequestParam(value = "distance", defaultValue = "2000") String distance){
        try {
            List<NearUserVo> nearUserVoList = this.todayBestService.queryNearUser(gender,distance);
            if (!CollectionUtils.isEmpty(nearUserVoList)){
                return ResponseEntity.ok(nearUserVoList);
            }
        } catch (Exception e) {
            log.error("搜附近错误 ~  gender :" + gender + ",distance=" + distance,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 探花
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList() {
        try {
            List<TodayBest> list = this.todayBestService.queryCardsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/love")
    public ResponseEntity<Void> likeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.todayBestService.likeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 不喜欢
     *
     * @param likeUserId
     * @return
     */
    @GetMapping("{id}/unlove")
    public ResponseEntity<Void> disLikeUser(@PathVariable("id") Long likeUserId) {
        try {
            this.todayBestService.disLikeUser(likeUserId);
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
